<?php

/**
 * Description of Comment Controller
 *
 * @author Vu
 */

class Admin_Cms_CommentController extends My_Controller_Action {
	
	public function init(){
		parent::init();
		
		$auth = Zend_Auth::getInstance();
        
        if(!$auth->hasIdentity())
        { 
            $this->_redirect("user/account/login");
        }
        
        if ($this->isAccess($auth->getIdentity()->group_id, 3))
        {
			//to do
		} else {
            die("Access denied");
        }
	}   
	
	public function indexAction() {			
		if($this->_request->isPost())
		{
			$checkbox = $_POST['checkbox'];
			$status = $_POST['status'];
			
			$model = $this->loadModel('admin_cms', 'Comment');
			for($i = 0; $i < count($checkbox); $i++)
			{
				$comment = $model->getCommentById($checkbox[$i]);
				$comment->status = $status;
				$comment->save();
			}
		}
		
        $this->view->rightAdminModules('right');
        
        $this->view->by = $this->_request->getParam("by", "c.created");
        $this->view->order = $this->_request->getParam("order", "desc");
        $this->view->filter = $this->_request->getParam("filter", "");
        
        Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
        $model = $this->loadModel('admin_cms', 'Comment');
        $this->view->paginator = $model->getAllComments($this->_getParam('page', 1), $this->view->by.' '.$this->view->order, $this->view->filter);
    }

    public function deleteAction() {
        $this->view->rightAdminModules('right');
        	
        $model = $this->loadModel('admin_cms', 'Comment');
        $commentid = $this->_request->getParam('id');
        $comment = $model->getCommentById($commentid);
        $comment->delete();

        $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "index"));
        $this->_redirect($redirect_url);
    }
    
	public function publicAction() {
        $this->view->rightAdminModules('right');
        	
        $model = $this->loadModel('admin_cms', 'Comment');
        $commentid = $this->_request->getParam('id');
        $model->setPublicComment($commentid);

        $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "index"));
        $this->_redirect($redirect_url);
    }
    
	public function privateAction() {
        $this->view->rightAdminModules('right');
        	
        $model = $this->loadModel('admin_cms', 'Comment');
        $commentid = $this->_request->getParam('id');
        $model->setPrivateComment($commentid);

        $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "index"));
        $this->_redirect($redirect_url);
    }
    
	public function detailAction() {
        $this->view->rightAdminModules('right');
        	        
        $this->view->errors = $this->_errorMessenger->getMessages();
        $this->_errorMessenger->clearMessages();

        $model = $this->loadModel('admin_cms', 'Comment');
        $commentid = $this->_request->getParam('id');
        $this->view->comment = $model->getCommentById($commentid);
    }

    public function editAction() {
        $this->view->rightAdminModules('right');
        	        
        $this->view->errors = $this->_errorMessenger->getMessages();
        $this->_errorMessenger->clearMessages();

        $model = $this->loadModel('admin_cms', 'Comment');
        $commentid = $this->_request->getParam('id');
        $this->view->comment = $model->getCommentById($commentid);
    }

    public function editpostAction() {
        if ($this->_request->isPost()) {
            if ($this->_request->getParam('name')) {
                $model = $this->loadModel('admin_cms', 'Comment');
                $commentid = $this->_request->getParam('comment_id');
				
                $comment = $model->getCommentById($commentid);
                $comment->name = $this->_request->getParam('name');
                $comment->save();

                $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "index"));
                $this->_redirect($redirect_url);
            } else {
                //$this->_errorMessenger->addMessage($this->view->translate("Please write commnet name"));
                $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "edit", "id" => $commentid));
                $this->_redirect($redirect_url);
            }
        } else {
            $redirect_url = $this->getUrl(array("module" => "admin_cms", "controller" => "comment", "action" => "index"));
            $this->_redirect($redirect_url);
        }
    }
}
